﻿/***
    Copyright 2012 Stijn Van Antwerpen

    This file is part of Qasparov.

    Qasparov is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Qasparov is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Qasparov. If not, see <http://www.gnu.org/licenses/>.

 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.Oxilly.Qsb.Core.SDK.Events;

namespace Com.Oxilly.Qsb.Core.Managers
{
    /// <summary>
    /// The StateManager is intended to keep track of the state of the engine and notify handlers when the state changes.
    /// The StateManager also was responsible to react on changes and switch between states in a lifecyling way.There this is 
    /// no longer the case We may consider deprecating the StateManager. Altought it may also be usefull to reïmplement this lifecycling 
    /// thought as it may be usefull.
    /// </summary>
    internal class StateManager : ManagerBase
    {
        private Engine engine;

        private EngineStates state = EngineStates.Init;

        public EngineStates State { get { return state; } }
    
        internal event EventHandler<StateChangedEventArgs> OnStateChanges;

        public StateManager(Engine engine)
        {
            this.engine = engine;
        }

        /// <summary>
        /// Internal methode to identify a change of state.
        /// This method does not invoke the actions assosiated with the change,
        /// therfore, the actual methode to invoke the change should be called.
        /// </summary>
        /// <param name="state">The new state of the engine.</param>
        internal void ChangeState(EngineStates state)
        {
            //Changing to the same state as the current state is not considderd a state change!
            //No events should trigger on that.
            if (this.state != state)
            {
                EngineStates oldState = this.state;
                this.state = state;
                engine.Log("State changed from " + oldState.ToString() + " to " + state.ToString());
                FireOnStateChanges(new StateChangedEventArgs { previousState = oldState, currentState = state });
            }
        }

        internal void FireOnStateChanges(StateChangedEventArgs e)
        {
            if (OnStateChanges != null) OnStateChanges(this, e);
        }


        internal override void ReleaseResources()
        {
           //This method is intentionally left empty. There are no resources to release.
        }
    }
}
